require.config({
	paths: {
        jquery: 'jquery-1.12.3.min'
    },
	shim: {
		'jquery':{
			deps: ['jquery-1.12.3.min'],
			exports: 'jquery'
		}
	}
});

define(['require', 'jquery'], function(require, $) {
	// 自定义简易模板引擎
	String.prototype.parseTemplate = function(obj) {
	    return this.replace(/\$\w+\$/gi, function(matchs) {
	        var returns = obj[matchs.replace(/\$/g, '')];		
	        return (returns + '') == 'undefined' ? '' : returns;
	    });
	}
	// ajax配置
	var ajaxRender = {
		'companyNews':'json/companyNews.json',// 公司动态
		'industryNews':'json/industryNews.json',// 行业动态
		'allFiles':'json/allFiles.json',// 各级文件
		'safetyInfor':'json/safetyInformation.json',//安全警示
		'safetyKnowledge':'json/safetyKnowledge.json',//航空安全知识
		'toDoTask':'json/toDoTask.json',//待办事宜
		'template-type-A':  '<li class="list-item">'+
								'<a href="$newsUrl$" target="_blank"><span class="glyphicon glyphicon-circle-arrow-right"></span>$news$</a>'+
							'</li>',
		'template-type-B':  '<li class="list-more">'+
					    		'<a class="btn btn-default" href="$moreNewsUrl$" target="_blank"><span class="glyphicon glyphicon-plus"></span>更多</a>'+
					    	'</li>'
	}
	/**
	 * 构建DOM
	 * @param {[type]}   options 			[参数]
	 * @param {Function} fn      			[初始化回调函数]
	 */
	function Render(options, fn){
		var defaultOptions = {
			newsHTML:ajaxRender['template-type-A'],//新闻列表
			moreHTML:ajaxRender['template-type-B']//更多按钮
		}
		this.options = $.extend(true, {}, defaultOptions, options);
		typeof fn === 'function' && fn instanceof Function && fn.apply(this, [this.options]);
	}
	Render.prototype.renderDOM = function(successOut){
		var options = this.options, that = this;
		$.ajax({
			url: options.url,
			// type: 'POST',
			dataType: 'json',
			data: options.data
		}).done(function(msg) {
			if(msg.statusCode!=200){ return; }
			
			var innerHTML = '', data = msg.data, map = msg.map, length=data.length;
			if(!length){
				options.$wrap.html( innerHTML ).css('background-image', 'url(img/nodata.jpg)');
				return;
			}
			for (var i = 0; i < length; i++) {
				innerHTML += options.newsHTML.parseTemplate(data[i]);
			}
			if(length>=options.limit){
				innerHTML += options.moreHTML.parseTemplate(map);
			}
			options.$wrap.html( innerHTML ).css('background-image', 'none');
		}).done(function(msg){//数据返回成功 对外提供扩展
			typeof successOut === 'function' && successOut instanceof Function && successOut.apply(that, [msg]);
		});
	}
	Render.prototype.close = function(){//可扩展
		this.options.$wrap[0].style.display='none';
	}
	Render.prototype.show = function(){//可扩展
		this.options.$wrap[0].style.display='block';
	}
	function creatRender(options, fn){
		return new Render(options, fn);
	}


// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	// 点击标题切换tab
	var click2tab = function(){
		$('.main-title').off('click').on('click', function(event){
			if($(this).children().length<=1){ return;}
			event.preventDefault();//备用
			event.stopPropagation();
			var target = event.target;
			if($(target).hasClass('active')){ return; }
			$(target).addClass('active').siblings().removeClass('active');
			$(this).siblings('.main-list-wrap').children().each(function(_index, _el) {
				_index==$(target).index() ? function(){
					$(_el).css('display', 'block');
				}() : function(){
					$(_el).css('display', 'none');
				}();
			});
		});
	}();

	// 公司动态
	creatRender({
		url: ajaxRender['companyNews'],//后台接口的地址
		data: {param1:'value1'},//ajax获取接口数据时可能需要传递的参数
		limit:8,//最多显示8条，若大于8条则显示更多按钮
		$wrap: $('.main-list-wrap *[data-list=company-news-list]')//jq插入数据的位置
	}, function(){
		this.renderDOM();
	});
	// 行业动态
	creatRender({
		url: ajaxRender['industryNews'],
		data: {param1:'value1'},
		limit:8,
		$wrap: $('.main-list-wrap *[data-list=industry-news-list]')
	}, function(){
		this.renderDOM();
		this.close();//默认不渲染，不显示，点击相应标题即可显示
	});
	// 各级文件
	creatRender({
		url: ajaxRender['allFiles'],
		data: {param1:'value1'},
		limit:8,
		$wrap: $('.main-list-wrap *[data-list=all-files-list]')
	}, function(){
		this.renderDOM();
		this.close();
	});
	// 安全警示
	creatRender({
		url: ajaxRender['safetyInfor'],
		data: {param1:'value1'},
		limit:8,
		$wrap: $('.main-list-wrap *[data-list=safety-infor-list]')
	}, function(){
		this.renderDOM();
	});
	//航空安全知识
	creatRender({
		url: ajaxRender['safetyKnowledge'],
		data: {param1:'value1'},
		limit:8,
		$wrap: $('.main-list-wrap *[data-list=safety-knowledge-list]')
	}, function(){
		this.renderDOM();
		this.close();
	});
	// 待办事宜
	creatRender({
		url: ajaxRender['toDoTask'],
		data: {param1:'value1'},
		limit:8,
		$wrap: $('.main-list-wrap *[data-list=todo-task-list]'),
		$title:$('.main-title *[data-title=todo-task-title]')
	}, function(){
		this.renderDOM(function(){
			var msg = arguments[0];
			this.options.$title.children('.badge').html(msg.totalRows).attr('title', msg.totalRows);//显示总消息数目
		});
	});
});